﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NewICMng.Model
{
    public class ExportExcel
    {
        /// <summary> 
        /// 将DataTable数据导出到EXCEL，调用该方法后自动返回可下载的文件流 
        /// </summary> 
        /// <param name="dt">要导出的数据源</param> 
        public static int exportExcel(DataTable dt, String filename)
        {
            GridView gvExport = null;
            // 当前对话 
            HttpContext curContext = HttpContext.Current;
            // IO用于导出并返回excel文件 
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                // 设置编码和附件格式 
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
                curContext.Response.Charset = "utf-8";
                // 导出excel文件 
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                // 为了解决gvData中可能进行了分页的情况，需要重新定义一个无分页的GridView 
                gvExport = new GridView();
                gvExport.DataSource = dt.DefaultView;
                gvExport.AllowPaging = false;
                gvExport.DataBind();
                // 返回客户端 
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
                //curContext.Response.Write(@"<style> .text { mso-number-format:\@; } </script> ");
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
                return 0;
            }
            else
            {
                return -1;
            }
        }
        /// <summary> 
        /// 将DataTable数据导出到EXCEL，调用该方法后自动返回可下载的文件流 
        /// </summary> 
        /// <param name="dt">要导出的数据源</param> 
        public static int exportExcel(DataTable dt, String filename,string adds)
        {
            GridView gvExport = null;
            // 当前对话 
            HttpContext curContext = HttpContext.Current;
            // IO用于导出并返回excel文件 
            StringWriter strWriter = null;
            HtmlTextWriter htmlWriter = null;
            if (dt != null && dt.Rows.Count > 0)
            {
                // 设置编码和附件格式 
                curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");
                curContext.Response.ContentType = "application/vnd.ms-excel";
                curContext.Response.ContentEncoding = Encoding.GetEncoding("gb2312");
                curContext.Response.Charset = "utf-8";
                // 导出excel文件 
                strWriter = new StringWriter();
                htmlWriter = new HtmlTextWriter(strWriter);
                // 为了解决gvData中可能进行了分页的情况，需要重新定义一个无分页的GridView 
                gvExport = new GridView();
                gvExport.DataSource = dt.DefaultView;
                gvExport.AllowPaging = false;
                gvExport.DataBind();
                // 返回客户端 
                gvExport.RenderControl(htmlWriter);
                curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />");
                //curContext.Response.Write(@"<style> .text { mso-number-format:\@; } </script> ");
                curContext.Response.Write(adds);
                curContext.Response.Write(strWriter.ToString());
                curContext.Response.End();
                return 0;
            }
            else
            {
                return -1;
            }
        }
    }
}